package com.heba.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.heba.greenmulu.R;
import com.heba.utils.DensityUtils;

/**

 * Created by jason on 2020/5/14.
 */
public class ClearEditTextView extends LinearLayout {

    private Context context;
    public EditText et_search;
    private ImageView iv_delete;

    private OnEditTextListener listener;

    /**
     *
     * @param listener
     */
    public void setOnEditTextListener(OnEditTextListener listener) {
        this.listener = listener;
    }

    public ClearEditTextView(Context context) {
        super(context);
        this.context = context;
        init(context, null);
    }

    public ClearEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context, attrs);
    }

    public ClearEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(context, attrs);
    }

    /**
     * 自定义布局
     *
     * @param context
     * @param attrs
     */
    private void init(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_clear_search, this);
        et_search = view.findViewById(R.id.edit_search);
        iv_delete = view.findViewById(R.id.iv_delete);

        iv_delete.setVisibility(View.INVISIBLE);

        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().isEmpty()) {
                    iv_delete.setVisibility(View.INVISIBLE);

                    if (null != listener) {
                        listener.onDeleteClick();
                    }
                } else {
                    iv_delete.setVisibility(View.VISIBLE);

                    if (null != listener) {
                        listener.afterTextChanged(s.toString());
                    }
                }
            }
        });

        iv_delete.setOnClickListener(v -> {
            et_search.setText("");
            v.setVisibility(View.INVISIBLE);

            if (null != listener) {
                listener.onDeleteClick();
            }
        });

        et_search.setOnClickListener(v -> {
            if (null != listener) {
                listener.onIntent();
            }
        });

        et_search.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (v.getText().toString().trim().isEmpty()) {
                    return false;
                }
                if (null != listener) {
                    listener.onSearchClick(v.getText().toString());
                }
            }
            return false;
        });

    }

    /**
     * 移除焦点
     */
    public void clearFocus() {
        et_search.setFocusable(false);
        et_search.setFocusableInTouchMode(false);
    }

    /**
     * 显示软键盘
     */
    public void showKeyBoard() {
        DensityUtils.showSoftKeyBoard(context, et_search);
    }

    /**
     * 隐藏软键盘
     */
    public void hideKeyBoard() {
        DensityUtils.hideSoftWindow(context, et_search);
    }

    /**
     * @param textHint
     */
    public void setTextHint(String textHint) {
        et_search.setHint(textHint);
    }

    public String getText() {
        return et_search.getText().toString().trim();
    }

    /**
     * interface
     */
    public interface OnEditTextListener {

        void onDeleteClick();

        void afterTextChanged(String text);

        void onSearchClick(String text);

        void onIntent();

    }

}
